package com.zero.shardingsphere.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zero.shardingsphere.entity.Course;
import com.zero.shardingsphere.entity.Dict;
import com.zero.shardingsphere.entity.User;
import com.zero.shardingsphere.mapper.CourseMapper;
import com.zero.shardingsphere.mapper.DictMapper;
import com.zero.shardingsphere.mapper.UserMapper;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CourseController {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private DictMapper dictMapper;

    @Resource
    private UserMapper userMapper;

   @Test
    public void addCourse(){

        for (int i=0;i<10;i++){
            Course course = new Course();
            course.setCname("java");
            course.setUserId(1000L+i);
            course.setCstatus("1");
            courseMapper.insert(course);
        }
    }

    @Test
    public void QueryCourses(){
        //Logic SQL: SELECT  cid,cname,user_id,cstatus  FROM course
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("cid");
        queryWrapper.eq("cid",681912788565622785l);
        List<Course> courses = courseMapper.selectList(queryWrapper);
        courses.forEach(course -> {
            System.out.println(course);
        });
    }

    @Test
    public void QueryRangeStandard(){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("cid",681912788565622785l,681912788737589249l);
        List<Course> courses = courseMapper.selectList(queryWrapper);
        courses.forEach(course -> {
            System.out.println(course);
        });
    }

    @Test
    public void queryCourseComplex(){
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.between("cid",681955710505844737l,681955710627479553l);
        wrapper.eq("user_id",1009L);
        List<Course> courses = courseMapper.selectList(wrapper);
        courses.forEach(course -> System.out.println(course));
    }

    @Test
    public void queryCourseByHint(){
        //只查询course_2,强制路由
        HintManager hintManager = HintManager.getInstance();
        hintManager.addTableShardingValue("course",2);
        List<Course> courses = courseMapper.selectList(null);
        courses.forEach(course -> System.out.println(course));
        hintManager.close();
    }

    @Test
    public void addDictBroadcast(){
        Dict d1 = new Dict();
        d1.setUstatus("1");
        d1.setUvalue("正常");
        dictMapper.insert(d1);

        Dict d2 = new Dict();
        d2.setUstatus("0");
        d2.setUvalue("不正常");
        dictMapper.insert(d2);
    }

    @Test
    public void addDictbindUser(){
        Dict d1 = new Dict();
        d1.setUstatus("1");
        d1.setUvalue("正常");
        dictMapper.insert(d1);

        Dict d2 = new Dict();
        d2.setUstatus("0");
        d2.setUvalue("不正常");
        dictMapper.insert(d2);

       for(int i = 0 ; i < 10 ; i ++){
            User user = new User();
            user.setUsername("user No "+i);
            user.setUstatus(""+(i%2));
            user.setUage(i*10);
            userMapper.insert(user);
        }
    }

    @Test
    public void queryUserStatus(){
        List<User> users = userMapper.queryUserStatus();
        users.forEach(user -> System.out.println(user));
    }

    @Test
    public void addDictByMS(){
        Dict d1 = new Dict();
        d1.setUstatus("1");
        d1.setUvalue("正常");
        dictMapper.insert(d1);

        Dict d2 = new Dict();
        d2.setUstatus("0");
        d2.setUvalue("不正常");
        dictMapper.insert(d2);
    }

    @Test
    public void queryDictByMS(){
        List<Dict> dicts = dictMapper.selectList(null);
        dicts.forEach(dict -> System.out.println(dict));
    }
}
